package com.shihan.rabbitmq.utils;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeoutException;

/**
 * @Description Rabbit MQ 的连接工具
 * @Author shihan
 * @Date 2021/1/2 10:14
 * @Version 1.0
 */
public class ConnUtil {

    // 声明 RabbitMQ 服务器所在地址信息
    public static final String URI = "amqp://root:123456@192.168.66.129:5672/%2f";

    private static Connection connection = null;

    private static Channel channel = null;

    public static Channel getChannel() throws NoSuchAlgorithmException, KeyManagementException, URISyntaxException, IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri(URI);

        connection = connectionFactory.newConnection();
        channel = connection.createChannel();
        return channel;
    }

    public static void closeConn() throws IOException, TimeoutException {
        channel.close();
        connection.close();
    }

}
